﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    /// <summary>
    /// 获取个人的推广活动列表
    /// </summary>
    class GetPromotionalActivityListAction : GetAction<Params.GetPromotionalActivityListParams,
        Results.GetPromotionalActivityListResult>
    {
        public static string[] paarr = new string[]{
            "./Res/badminton_min.jpg",
            "./Res/basketball_min.jpg",
            "./Res/football_min.jpg"
        };
        public static string[] paalinkrr = new string[]{
            "./HTML/Prompt_1.html",
            "./HTML/Prompt_1.html",
            "./HTML/Prompt_1.html"
        };

        protected override ActionResult<Results.GetPromotionalActivityListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            Params.GetPromotionalActivityListParams opParams)
        {
            int limit = 10;
            int start = opParams.Page * limit;
            ServiceResult<PagedList<PromotionalActivity>> result = new PromotionalActivityService().GetPagedList(opParams.City, start, limit);

            if (result.Data.Items.Count == 0)
            {
                // 没有取到该城市就取默认的
                result = new PromotionalActivityService().GetPagedList("-", start, limit);
            }
            Results.GetPromotionalActivityListResult actionResult = new Results.GetPromotionalActivityListResult();
            actionResult.Start = result.Data.Start;
            actionResult.Limit = result.Data.Limit;
            actionResult.Total = result.Data.Total;

            for (int index = 0; index < result.Data.Items.Count; index++)
            {
                Results.GetPromotionalActivityListResult.Item item = new Results.GetPromotionalActivityListResult.Item();
                item.Id = result.Data.Items[index].Id;
                item.CreatedTime = result.Data.Items[index].CreatedTime;
                item.BeginTime = result.Data.Items[index].BeginTime;
                item.EndTime = result.Data.Items[index].EndTime;
                item.Title = result.Data.Items[index].Title;
                if (!string.IsNullOrEmpty(result.Data.Items[index].PreviewImageUrl))
                {
                    if(result.Data.Items[index].PreviewImageUrl.StartsWith("."))
                    {
                        item.PreviewImageUrl = string.Format("{0}{1}", Configs.BaseWebUrl, result.Data.Items[index].PreviewImageUrl);
                    }
                    else
                    {
                        item.PreviewImageUrl = result.Data.Items[index].PreviewImageUrl;
                    }
                }
                if (!string.IsNullOrEmpty(result.Data.Items[index].LinkUrl))
                {
                    if (result.Data.Items[index].LinkUrl.StartsWith("."))
                    {
                        item.LinkUrl = string.Format("{0}{1}", Configs.BaseWebUrl, result.Data.Items[index].LinkUrl);
                    }
                    else
                    {
                        item.LinkUrl = result.Data.Items[index].LinkUrl;
                    }
                }

                actionResult.Items.Add(item);
            }

            if (result.Data.Start + result.Data.Items.Count >= result.Data.Total)
            {
                actionResult.IsTheEnd = true;
            }
            else
            {
                actionResult.IsTheEnd = false;
            }

            return new ActionResult<Results.GetPromotionalActivityListResult>("1.0.1", true,
                ErrorCode.NoError,
                ErrorCode.ToString(ErrorCode.NoError),
                actionResult);
        }
    }
}
